﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace WssClient.Item.XinDaXiang
{
    public class Product
    {
        private int idx;    //序号
        private string info = "";    //商品信息
        private int packages = 0;   //出库件数
        private decimal unitsNumber = 0;      //单位数量
        private decimal number = 0;     //出库数量
        private string units = "";      //单位
        private decimal price = 0;  //单价
        private decimal total = 0;      //合计
        private string remark = "";      //备注
        private List<string> details = new List<string>();      //明细列表

        public int Idx { get => idx; set => idx = value; }
        public string Info { get => info; set => info = value; }
        public int Packages { get => packages; set => packages = value; }
        public decimal UnitsNumber { get => unitsNumber; set => unitsNumber = value; }
        public decimal Number { get => number; set => number = value; }
        public string Units { get => units; set => units = value; }
        public decimal Price { get => price; set => price = value; }
        public decimal Total { get => total; set => total = value; }
        public string Remark { get => remark; set => remark = value; }
        public List<string> Details { get => details; set => details = value; }

        /// <summary>
        /// 解析数据：跑马重产品数据
        /// </summary>
        /// <param name="data">未转化产品数据对象</param>
        /// <returns></returns>
        public static Product AnalysisMonkeyHeavy(dynamic data)
        {
            Product product = new Product();
            try
            {
                product.Info = CommonHelper.GetString(data, "mh_info");
                product.Packages = CommonHelper.GetInt(data, "mh_packages");
                product.UnitsNumber = CommonHelper.GetDecimal(data, "mh_units");
                product.Number = CommonHelper.GetDecimal(data, "mh_number");
                product.Units = CommonHelper.GetString(data, "mh_unit");
                product.Price = CommonHelper.GetDecimal(data, "mh_price");
                product.Total = CommonHelper.GetDecimal(data, "mh_total");
                product.Remark = CommonHelper.GetString(data, "mh_remark");
                string detailStr = CommonHelper.GetString(data, "mh_detail");

                //每行10个数值，超过自动换行
                string[] detailArr = detailStr.Split(' ');
                int _length = detailArr.Length;
                int _idx = 0;
                int _endx = _length > 12 ? 11 : _length - 1;

                while (_endx > -1 && _idx < _length)
                {
                    string _val = "";
                    do
                    {
                        _val += $"{detailArr[_idx]} ";
                    } while (_idx++ < _endx);
                    product.Details.Add(_val);
                    _endx = _length > _idx + 12 ? _idx + 11 : _length - 1;
                }
            }
            catch (Exception e)
            {

            }
            return product;
        }

        /// <summary>
        /// 解析数据：常规产品数据
        /// </summary>
        /// <param name="data">未转化产品数据对象</param>
        /// <returns></returns>
        public static Product AnalysisGeneral(dynamic data)
        {
            Product product = new Product();
            try
            {
                product.Info = CommonHelper.GetString(data, "g_info");
                product.Packages = CommonHelper.GetInt(data, "g_packages");
                product.UnitsNumber = CommonHelper.GetDecimal(data, "g_units");
                product.Number = CommonHelper.GetDecimal(data, "g_number");
                product.Units = CommonHelper.GetString(data, "g_unit");
                product.Price = CommonHelper.GetDecimal(data, "g_price");
                product.Total = CommonHelper.GetDecimal(data, "g_total");
                product.Remark = CommonHelper.GetString(data, "g_remark");
            }
            catch (Exception e)
            {

            }
            return product;
        }
    }
}
